package com.baizhi.cmfz.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "myFilter",urlPatterns = {"/main/*","/jsp/*"})
public class LoginFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {


        System.out.println("filter__________________________");
        HttpServletRequest httpRequest= (HttpServletRequest) req;
        HttpServletResponse httpResponse= (HttpServletResponse) resp;

        //从session中获取登录页标识，登录则放行
        HttpSession session=httpRequest.getSession();
        Object o = session.getAttribute("login");
        boolean login=o!=null?true:false;

        if(true){  // login
            filterChain.doFilter(req,resp);
        }else{
            //否则重定向到登录页面
            httpResponse.sendRedirect("/cmfz/login.jsp");
        }

    }

    @Override
    public void destroy() {

    }
}
